package fileupdownYouhua.web;

import cn.hutool.core.util.StrUtil;
import fileupdownYouhua.dao.DatabaseDaoIml;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.service(req, resp);
        //校验参数
        boolean search = StrUtil.isEmpty(req.getParameter("fileName"));
        String fileName = null ;
        if (!search){
            fileName = req.getParameter("fileName");
        }

        //从数据库查询到的存放三个集合的集合
        List<List> listList = new DatabaseDaoIml().serach(search, fileName);

        // 设置分页参数
        int pageSize = 5; // 每页显示的记录数
        int currentPage = 1; // 当前页数，默认为第一页
        String pageParam = req.getParameter("page");
        if (pageParam != null && !pageParam.isEmpty()) {
            currentPage = Integer.parseInt(pageParam);
        }
        // 计算分页的起始索引和结束索引
        int startIndex = (currentPage - 1) * pageSize;
        //获取最后一条数据
        int endIndex = Math.min(startIndex + pageSize, listList.get(0).size());
        // 获取当前页的学生列表
        List<String> list1 = listList.get(0).subList(startIndex, endIndex);
        List<String> list2 = listList.get(1).subList(startIndex, endIndex);
        List<String> list3 = listList.get(2).subList(startIndex, endIndex);
        req.setAttribute("currentPage", currentPage);
        //获取总页数
        req.setAttribute("totalPages", (int) Math.ceil((double) listList.get(0).size() / pageSize));
        req.setAttribute("nameList", list1);
        req.setAttribute("dataList", list2);
        req.setAttribute("idList", list3);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
}
